package io.gitee.devaper.starlight.common.util

/*
    这里存放与Uri相关的工具
 */

import java.util.LinkedList
import java.util.StringTokenizer

/**
 * 解析原始的queryString为Map
 *
 * @param rawQuery rawQueryString
 */
fun parseRawQuery(rawQuery: String): Map<String, List<String>> {
    val query = if (rawQuery.startsWith('?')) rawQuery.substring(1) else rawQuery

    val map = HashMap<String, MutableList<String>>()
    val tokenizer = StringTokenizer(query, "&")
    while (tokenizer.hasMoreTokens()) {
        val token = tokenizer.nextToken()
        val delimIndex = token.indexOf('=')

        if (delimIndex == -1) {
            map.getOrPut(token) { LinkedList() }
        } else {
            val key = token.substring(0, delimIndex)
            val value = token.substring(delimIndex + 1)
            map.getOrPut(key) { LinkedList() }.add(value)
        }
    }

    return map
}